package modele;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class CSV {

	private static final String FILE = "./data.csv";

	/**
	 * save the contacts when leaving the application
	 */
	public static void saveContacts() {
		try {
			FileWriter writer = new FileWriter(FILE);
			for (int i = 0; i < Contact.getTab().size(); ++i) {
				writer.append(Contact.getTab().get(i).getFirstName());
				writer.append(';');
				writer.append(Contact.getTab().get(i).getLastname());
				writer.append(';');

				if (!Contact.getTab().get(i).getHomeAddress().equals("")) {
					writer.append(Contact.getTab().get(i).getHomeAddress());
				} else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getCompanyAddress().equals("")) {
					writer.append(Contact.getTab().get(i).getCompanyAddress());
				} else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getPersonalPhone().equals("")) {
					writer.append(Contact.getTab().get(i).getPersonalPhone());
				} else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getProfessionalPhone().equals("")) {
					writer.append(Contact.getTab().get(i)
							.getProfessionalPhone());
				} else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getHomePage().equals("")) {
					writer.append(Contact.getTab().get(i).getHomePage());
				} else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getEmail().equals(""))
					writer.append(Contact.getTab().get(i).getEmail());
				else
					writer.append(" ");
				writer.append(';');

				if (!Contact.getTab().get(i).getPhoto().equals(""))
					writer.append(Contact.getTab().get(i).getPhoto());
				else
					writer.append(" ");
					writer.append(';');
				
				if (!Contact.getTab().get(i).getGroup().equals(""))
					writer.append(Contact.getTab().get(i).getGroup());
				else
					writer.append(" ");
				writer.append('\n');

			}
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * get the saved contacts when launching the application
	 */
	public static void loadContacts() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(FILE));
			String line = "";
			String separator = ";";

			while ((line = br.readLine()) != null) {

				String[] info = line.split(separator);
				ArrayList<String> list = new ArrayList<String>();

				for (int i = 0; i < 10; ++i) {
					if (!info[i].equals(" "))
						list.add(info[i]);
					else
						list.add("");

				}
				Contact.getTab().add(new Contact(list));
			}
			br.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
